package com.xidian.zhiancloud.service.lz.hiddeninvestigation.hiddeninvestigationbasic;

import com.xidian.zhiancloud.dao.HiddenBasicMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.HiddenBasic;
import com.xidian.zhiancloud.model.HiddenBasicExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HiddenInvestigationService {

    @Autowired
    private HiddenBasicMapper hiddenBasicMapper;

    public List<HiddenBasic> getList(String investigationName, int current, int size) {
        HiddenBasicExample example = new HiddenBasicExample();
        HiddenBasicExample.Criteria criteria = example.createCriteria();
        if (investigationName!=null || "".equals(investigationName)) {
            criteria.andInvestigationNameLike('%' + investigationName + '%');
        }
        return hiddenBasicMapper.selectByExampleWithRowbounds(example, new RowBounds(current, size));

    }

    public void insert(HiddenBasic hiddenBasic) {
        int count = hiddenBasicMapper.insert(hiddenBasic);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void update(HiddenBasic hiddenBasic) {
        HiddenBasicExample example = new HiddenBasicExample();
        HiddenBasicExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(hiddenBasic.getId());
        HiddenBasic hidden = new HiddenBasic();
        hidden.setInvestigationName(hiddenBasic.getInvestigationName());
        int count = hiddenBasicMapper.updateByExampleSelective(hidden, example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void delete(HiddenBasic hiddenBasic) {
        HiddenBasicExample example = new HiddenBasicExample();
        HiddenBasicExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(hiddenBasic.getId());

        int count = hiddenBasicMapper.deleteByExample(example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void deleteByIds(Long[] ids) {
        for (Long id : ids) {
            int result = hiddenBasicMapper.deleteByPrimaryKey(id);
            if (result == 0) {
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }
    }
}
